<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="dataForm.userId" :disabled="disabled" placeholder="用户id"></el-input>
      </el-form-item>
      <el-form-item label="卡卷id" prop="cardId">
        <el-input v-model="dataForm.cardId" :disabled="disabled" placeholder="卡卷id"></el-input>
      </el-form-item>
      <el-form-item label="卡卷编码" prop="cardSn">
        <el-input v-model="dataForm.cardSn" :disabled="disabled" placeholder="卡卷编码"></el-input>
      </el-form-item>
      <el-form-item label="状态，0未使用，1已使用，2已失效" prop="status">
        <el-input v-model="dataForm.status" :disabled="disabled" placeholder="状态，0未使用，1已使用，2已失效"></el-input>
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-input v-model="dataForm.updateTime" :disabled="disabled" placeholder="更新时间"></el-input>
      </el-form-item>
      <el-form-item label="卡卷购买时间" prop="addTime">
        <el-input v-model="dataForm.addTime" :disabled="disabled" placeholder="卡卷购买时间"></el-input>
      </el-form-item>
      <el-form-item label="卡密类型" prop="type">
        <el-input v-model="dataForm.type" :disabled="disabled" placeholder="卡密类型"></el-input>
      </el-form-item>
      <el-form-item label="卡卷有效期限" prop="expireTime">
        <el-input v-model="dataForm.expireTime" :disabled="disabled" placeholder="卡卷有效期限"></el-input>
      </el-form-item>
      <el-form-item label="券面价值" prop="marketPrice">
        <el-input v-model="dataForm.marketPrice" :disabled="disabled" placeholder="券面价值"></el-input>
      </el-form-item>
      <el-form-item label="售价" prop="retailPrice">
        <el-input v-model="dataForm.retailPrice" :disabled="disabled" placeholder="售价"></el-input>
      </el-form-item>
      <el-form-item label="支付方式，1微信，1余额，3银联,4抽奖获得" prop="payType">
        <el-input v-model="dataForm.payType" :disabled="disabled" placeholder="支付方式，1微信，1余额，3银联,4抽奖获得"></el-input>
      </el-form-item>
      <el-form-item label="对应订单id" prop="orderId">
        <el-input v-model="dataForm.orderId" :disabled="disabled" placeholder="对应订单id"></el-input>
      </el-form-item>
      <el-form-item label="微信卡券id" prop="couponId">
        <el-input v-model="dataForm.couponId" :disabled="disabled" placeholder="微信卡券id"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        disabled: false,
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          cardId: '',
          cardSn: '',
          status: '',
          updateTime: '',
          addTime: '',
          type: '',
          expireTime: '',
          marketPrice: '',
          retailPrice: '',
          payType: '',
          orderId: '',
          couponId: ''},
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/usercard/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.usercard
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/mall/usercard/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
